package com.wzdigit.wms.wms.service.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusSkuCateRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.SkuCateResponse;
import com.wzdigit.wms.basic.domain.basic.SkuCate;
import com.wzdigit.wms.basic.enums.BaseIsEnabledEnum;
import com.wzdigit.wms.basic.mapper.core.basic.SkuCateMapper;
import com.wzdigit.wms.common.utils.DozerUtils;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.util.StringUtils;
import com.wzdigit.wms.common.entity.StatusCode;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 货品类别-服务类
 * @author yangdongjie
 * @SkuCateService.java
 * @2021年1月19日下午2:22:07
 */
@Service
public class SkuCateService {

	@Autowired
    private SkuCateMapper skuCateMapper;
	
	
	public List<SkuCateResponse> list() {
		QueryWrapper<SkuCate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("IS_ENABLED", BaseIsEnabledEnum.USE.code);
		List<SkuCate> list = skuCateMapper.selectList(queryWrapper);
		return DozerUtils.mapList(list, SkuCateResponse.class);
    }
	
	@SuppressWarnings("rawtypes")
	public PagerInfo<List<SkuCateResponse>> searchSkuCate(SearchSkuCateRequest dto) {
		IPage<SkuCate> page = new Page<>(dto.getSkipCount(), dto.getMaxResultCount());
		
		QueryWrapper<SkuCate> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotNull(dto.getSkuCateCode())) {
			queryWrapper.like("SKU_CATE_CODE", dto.getSkuCateCode());
		}
		if (StringUtils.isNotNull(dto.getEnvLevel())) {
			queryWrapper.like("ENV_LEVEL", dto.getEnvLevel());
		}
		if (dto.getIsEnabled() != null) {
			queryWrapper.like("IS_ENABLED", dto.getIsEnabled());
		}
		if (StringUtils.isNotNull(dto.getFilter())) {
			queryWrapper.and(wrapper -> {
				wrapper.like("SKU_CATE_CODE", dto.getFilter());
				wrapper.or().like("ENV_LEVEL", dto.getFilter());
			});
		}
		page = skuCateMapper.selectPage(page, queryWrapper);
		List<SkuCateResponse> list = DozerUtils.mapList(page.getRecords(), SkuCateResponse.class);
		
		PagerInfo<List<SkuCateResponse>> pagerInfo = new PagerInfo<List<SkuCateResponse>>();
		pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
	
	public SkuCateResponse getSkuCate(Integer skuCateId) {
		SkuCate SkuCate = skuCateMapper.selectById(skuCateId);
		SkuCateResponse response = new SkuCateResponse();
    	BeanUtil.copyNonNull(SkuCate, response);
		return response;
    }
	
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public SkuCateResponse createSkuCate(CreateSkuCateRequest dto,
    		HttpServletRequest servletRequest) {
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		
		QueryWrapper<SkuCate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("SKU_CATE_CODE", dto.getSkuCateCode());
		SkuCate skuCate = skuCateMapper.selectOne(queryWrapper);
		if (skuCate != null) {
			// 编码已存在
    		Shift.fatal(StatusCode.CODE_DATA_EXIST);
		}
		
		skuCate = new SkuCate();
    	BeanUtil.copyNonNull(dto, skuCate);
    	skuCate.setCreateUser(loginName);
    	skuCate.setCreateTime(new Date());
    	skuCateMapper.insert(skuCate);
    	
    	SkuCateResponse response = new SkuCateResponse();
    	BeanUtil.copyNonNull(skuCate, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public SkuCateResponse updateSkuCate(UpdateSkuCateRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
		
		QueryWrapper<SkuCate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("SKU_CATE_CODE", dto.getSkuCateCode());
		SkuCate skuCate = skuCateMapper.selectOne(queryWrapper);
		if (skuCate != null) {
			if(skuCate.getSkuCateId().intValue() != dto.getSkuCateId().intValue()) {
				// 编码已存在
				Shift.fatal(StatusCode.CODE_DATA_EXIST);
			}
		}
		
		skuCate = skuCateMapper.selectById(dto.getSkuCateId());
    	BeanUtil.copy(dto, skuCate);
    	skuCate.setModifyUser(loginName);
    	skuCate.setModifyTime(new Date());
    	skuCateMapper.updateById(skuCate);
    	
    	SkuCateResponse response = new SkuCateResponse();
    	BeanUtil.copyNonNull(skuCate, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> updateStatus(UpdateStatusSkuCateRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	
    	SkuCate skuCate = new SkuCate();
    	skuCate.setSkuCateId(dto.getSkuCateId());
    	skuCate.setIsEnabled(dto.getIsEnabled());
    	skuCate.setModifyUser(loginName);
    	skuCate.setModifyTime(new Date());
    	skuCateMapper.updateById(skuCate);
		return new Result<>();
    }
}
